//
//  AuthenticationViewController.m
//  FaceDiscernmentDemo
//
//  Created by mac on 2019/2/27.
//  Copyright © 2019年 mac. All rights reserved.
//

#import "AuthenticationViewController.h"
#import "FaceScanViewController.h"
#import "UIImage+Additions.h"

@interface AuthenticationViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *nameTextf;
@property (nonatomic,strong) UITextField *idCardText;

@property (nonatomic,strong) UIButton *photoBtnA;
@property (nonatomic,strong) UIImageView *photoImageB;

@property (nonatomic,strong) UIButton *commitBtn;
@property (nonatomic,strong) UILabel *tipsLabel;

@property (nonatomic, readwrite, weak) UIImageView *selectImageView;


@end

@implementation AuthenticationViewController

- (void)viewDidLoad { 
    [super viewDidLoad];
    
    [self setUI];
    // Do any additional setup after loading the view.
}

#pragma mark -- Action
- (void)photoButton:(UIButton *)sender{
    __weak typeof(self) weakSelf = self;
    if (sender.tag == 0) {
        self.selectImageView = self.photoImageB;
        FaceScanViewController *faceScan = [[FaceScanViewController alloc]init];
        faceScan.successCompletion = ^(NSDictionary * _Nonnull imagesDict, UIImage * _Nonnull originImage) {
            weakSelf.selectImageView.hidden = NO;
            weakSelf.selectImageView.image = originImage;
        };
        [weakSelf presentViewController:faceScan animated:YES completion:nil];
    }
}

- (void)commitButton:(UIButton *)sender{
    if (self.nameTextf.text.length == 0 || self.idCardText.text.length == 0) {
        self.tipsLabel.text = @"请输入姓名或身份证号";
        return;
    }
    
    NSDictionary* parm = @{@"access_token":[FaceAppToolHandle shareToolHandle].accessToken,
                           @"name":_nameTextf.text,
                           @"image":[self.photoImageB.image base64EncodedString],
                           @"image_type": @"BASE64",
                           @"id_card_number":_idCardText.text,
                           @"quality_control":@"LOW",
                           @"liveness_control":@"LOW"};
    NSTimeInterval start = [[NSDate date] timeIntervalSince1970];
    NSLog(@"start = %f",start);
    [WebConnect webPOSTConnectWithWithStrUrl:Baidu_getPersonVerifyURL pramaters:parm success:^(id responseObject) {
        NSLog(@"身份验证=%@",responseObject);
        NSTimeInterval end = [[NSDate date] timeIntervalSince1970];
        NSLog(@"end = %f",end);
        NSLog(@"公安 = %f",end - start);
        if ([responseObject[@"error_code"] intValue] == 0) {
            self.tipsLabel.text = [NSString stringWithFormat:@"相似度分值：%0.2f",[responseObject[@"result"][@"score"] floatValue]];
        } else {
            self.tipsLabel.text = responseObject[@"error_msg"]?[NSString stringWithFormat:@"错误信息：%@",responseObject[@"error_msg"]]:@"网络请求错误";
        }
    } failure:^(id failure) {
        NSLog(@"失败%@",failure);
    }];
    
}

#pragma mark -- UI
- (void)setUI{
    _nameTextf = [[UITextField alloc]init];
    _nameTextf.placeholder = @"请输入身份证上的姓名";
    _nameTextf.font = [UIFont systemFontOfSize:15];
    _nameTextf.delegate = self;
    _nameTextf.returnKeyType =UIReturnKeyDone;
    _nameTextf.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_nameTextf];
    [_nameTextf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        make.top.mas_equalTo(self.view.mas_top).offset(100);
        make.right.mas_equalTo(self.view.mas_right).offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    _idCardText = [[UITextField alloc]init];
    _idCardText.placeholder = @"请输入身份证上的身份证号";
    _idCardText.font = [UIFont systemFontOfSize:15];
    _idCardText.returnKeyType =UIReturnKeyDone;
    _idCardText.delegate = self;
    _idCardText.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_idCardText];
    [_idCardText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        make.top.mas_equalTo(_nameTextf.mas_bottom).offset(40);
        make.right.mas_equalTo(self.view.mas_right).offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    //
    _photoImageB = [[UIImageView alloc]init];
    _photoImageB.image = [UIImage imageNamed:@"add_photo_h"];
    [self.view addSubview:_photoImageB];
    [_photoImageB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_idCardText.mas_bottom).offset(30);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(140);
    }];
    //
    _photoBtnA = [[UIButton alloc]init];
    [_photoBtnA setTitle:@"选择图片" forState:UIControlStateNormal];
    _photoBtnA.backgroundColor = [UIColor grayColor];
    [_photoBtnA addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];
    _photoBtnA.tag = 0;
    [self.view addSubview:_photoBtnA];
    [_photoBtnA mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.photoImageB.mas_bottom).offset(30);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(35);
    }];
    
    //、、、、、、
    _tipsLabel = [[UILabel alloc]init];
    _tipsLabel.text = @"请传入进行对比的图片";
    _tipsLabel.textColor = [UIColor blackColor];
    _tipsLabel.textAlignment = NSTextAlignmentCenter;
    _tipsLabel.font = [UIFont systemFontOfSize:13];
    _tipsLabel.numberOfLines = 2;
    [self.view addSubview:_tipsLabel];
    [_tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(self.photoBtnA.mas_bottom).offset(100);
        make.height.mas_equalTo(40);
    }];
    
    _commitBtn = [[UIButton alloc]init];
    [_commitBtn setTitle:@"开始对比" forState:UIControlStateNormal];
    [_commitBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _commitBtn.backgroundColor = [UIColor orangeColor];
    [_commitBtn addTarget:self action:@selector(commitButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_commitBtn];
    [_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.tipsLabel.mas_bottom).offset(5);
        make.width.mas_equalTo(220);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark -- 撤销键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark -- UITextView Return响应键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
